'use strict';

const Controller = require('egg').Controller;

class StudentController extends Controller {
  // 查看学生数据
  async index() {
    let studentList = await this.app.model.Student.findAll()
    this.ctx.body = studentList
  }
  // 添加学生数据
  async create() {
    let name = this.ctx.request.body.name;
    let achievement = this.ctx.request.body.achievement;
    let clazz_id = this.ctx.request.body.clazz_id;
    // this.app.model.Clazz.create({name:“xx”}); //添加数据
    await this.app.model.Student.create({
      name: name,
      achievement: achievement,
      clazz_id: clazz_id
    })
    this.ctx.body = {
      name: name,
      achievement: achievement,
      clazz_id: clazz_id,
      msg: "添加成功"
    }
  }
  // 删除学生数据
  async destroy() {
    let id = this.ctx.params.id;
    await this.app.model.Student.destroy({
      where: {
        id: id
      }
    })
    this.ctx.body = "删除成功"
  }
  // 修改学生数据
  async update() {
    let id = this.ctx.params.id;
    let name = this.ctx.request.body.name;
    let achievement = this.ctx.request.body.achievement;
    let clazz_id = this.ctx.request.body.clazz_id;
    await this.app.model.Student.update({
      name: name,
      achievement: achievement,
      clazz_id
    }, { where: { id: id } })
    this.ctx.body = "修改成功"
  }

}

module.exports = StudentController;
